class Solution:
    def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
        result=[]
        if len(intervals)==0:
            return [newInterval]
        left=min([intervals[0][0],newInterval[0]])
        right=max([intervals[-1][1],newInterval[1]])
        mapper=[0 for _ in range(right-left+1)]
        for i in intervals:
            mapper[i[0]-left]+=1
            mapper[i[1]-left]+=2
        mapper[newInterval[0]-left]+=1
        mapper[newInterval[1]-left]+=2
        x=0
        while x<len(mapper):
            if mapper[x]!=0 and mapper[x]!=3:
                temp=x
                stack=1
                if mapper[x]==2:
                    stack+=1
                while stack!=0 and x<len(mapper)-1:
                    x+=1
                    if mapper[x]==1:
                        stack+=1
                    elif mapper[x]==2 or mapper[x]==5:
                        stack-=1
                    elif mapper[x]==4:
                        stack-=2             
                result.append([temp+left,x+left])
            elif mapper[x]==3:
                result.append([x+left,x+left])
            x+=1
        if len(result)==0:
            result.append([1,max([intervals[-1][1],newInterval[1]])])
        return result


'''
执行用时：
56 ms
, 在所有 Python3 提交中击败了
26.55%
的用户
内存消耗：
15.2 MB
, 在所有 Python3 提交中击败了
5.69%
的用户
'''